package middleware

import (
	"net/http"

	"go-gin-api/configs"
	"go-gin-api/internal/pkg/core"
	"go-gin-api/pkg/errno"
)

func (m *middleware) Token(ctx core.Context) (userId int64, userName string, err errno.Error) {
	token := ctx.GetHeader(configs.HeaderLoginToken)
	if token == "" {
		err = errno.NewError(
			http.StatusUnauthorized,
			"Header 中缺少 Token 参数")

		return
	}

	type userInfo struct {
		Id       int64  `json:"id"`       // 用户ID
		Username string `json:"username"` // 用户名
	}

	var userData userInfo

	userId = userData.Id
	userName = userData.Username

	return
}
